import { request } from '@/utils/request';

// 后端基础URL - 使用相对路径，通过Vite代理
const BASE_URL = '';

/**
 * 飞手注册接口
 */
export const registerPilot = (data: any) => {
  console.log('飞手注册请求数据:', data);
  console.log('请求URL:', `${BASE_URL}/drone/register/pilot`);
  console.log('请求方法: POST');
  console.log('请求头:', {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  });
  
  return request({
    url: `/drone/register/pilot`,
    method: 'POST',
    data: data,
    header: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
    },
    loadTime: true // 显示加载动画
  });
};

/**
 * 组织注册接口
 */
export const registerOrganization = (data: any) => {
  console.log('组织注册请求数据:', data);
  console.log('请求URL:', `${BASE_URL}/drone/register/organization`);
  
  return request({
    url: `/drone/register/organization`,
    method: 'POST',
    data: data,
    header: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    loadTime: true // 显示加载动画
  });
};

/**
 * 检查手机号是否唯一
 */
export const checkPhone = (phone: string) => {
  return request({
    url: `/drone/register/checkPhone/${phone}`,
    method: 'GET'
  });
};

/**
 * 检查用户名是否唯一
 */
export const checkUserName = (userName: string) => {
  return request({
    url: `/drone/register/checkUserName/${userName}`,
    method: 'GET'
  });
};

/**
 * 检查组织名称是否唯一
 */
export const checkOrgName = (orgName: string) => {
  return request({
    url: `/drone/register/checkOrgName/${orgName}`,
    method: 'GET'
  });
};

/**
 * 检查年龄是否有效
 */
export const checkAge = (age: string) => {
  return request({
    url: `/drone/register/checkAge/${age}`,
    method: 'GET'
  });
};

/**
 * 检查飞行年限是否有效
 */
export const checkFlightYears = (flightYears: string) => {
  return request({
    url: `/drone/register/checkFlightYears/${flightYears}`,
    method: 'GET'
  });
};

/**
 * 测试后端连接
 */
export const testConnection = () => {
  console.log('测试后端连接URL:', `${BASE_URL}/drone/register/checkPhone/13800138000`);
  return request({
    url: `${BASE_URL}/drone/register/checkPhone/13800138000`,
    method: 'GET',
    loadTime: false // 不显示加载动画，避免干扰
  });
};

/**
 * 测试注册接口是否存在
 */
export const testRegisterEndpoint = () => {
  console.log('测试注册接口URL:', `${BASE_URL}/drone/register/pilot`);
  return request({
    url: `/drone/register/pilot`,
    method: 'GET', // 使用GET方法测试接口是否存在
    loadTime: false
  });
};

/**
 * 测试注册接口 - 使用POST方法
 */
export const testRegisterEndpointPost = () => {
  console.log('测试注册接口POST URL:', `${BASE_URL}/drone/register/pilot`);
  return request({
    url: `/drone/register/pilot`,
    method: 'POST',
    data: {}, // 空数据测试
    header: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    loadTime: false
  });
};

/**
 * 测试注册接口 - 使用PUT方法
 */
export const testRegisterEndpointPut = () => {
  console.log('测试注册接口PUT URL:', `${BASE_URL}/drone/register/pilot`);
  return request({
    url: `/drone/register/pilot`,
    method: 'PUT',
    data: {}, // 空数据测试
    header: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    loadTime: false
  });
};

/**
 * 测试注册接口 - 使用不同的请求头
 */
export const testRegisterEndpointWithDifferentHeaders = () => {
  console.log('测试注册接口不同请求头 URL:', `${BASE_URL}/drone/register/pilot`);
  return request({
    url: `/drone/register/pilot`,
    method: 'POST',
    data: {}, // 空数据测试
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': '*/*'
    },
    loadTime: false
  });
};

/**
 * 最简单的连接测试 - 直接访问后端根路径
 */
export const testBasicConnection = () => {
  console.log('测试基本连接URL:', `${BASE_URL}`);
  return request({
    url: `/`,
    method: 'GET',
    loadTime: false
  });
};

/**
 * 测试不同端口
 */
export const testDifferentPorts = () => {
  const ports = [8080, 8081, 8082, 3000, 9000];
  const tests = ports.map(port => {
    const url = `http://localhost:${port}`;
    console.log(`测试端口 ${port}:`, url);
    return request({
      url: url,
      method: 'GET',
      loadTime: false
    }).catch(err => {
      console.log(`端口 ${port} 失败:`, err.statusCode);
      return { port, error: err.statusCode };
    });
  });
  return Promise.allSettled(tests);
};
